#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QFile>
#include <QMessageBox>
#include <QSharedPointer>

#include "pcinfo.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->actionChinese->setCheckable(true);
    ui->actionEngLish->setCheckable(true);
    // connect(ui->actionChinese,SIGNAL(triggered(bool)), this, SLOT(TransToChinese(bool)));
    // connect(ui->actionEngLish,SIGNAL(triggered(bool)), this, SLOT(TransToEnglish(bool)));
    // connect(ui->pushButton,SIGNAL(pressed()), this, SLOT(TransToNext()));

    m_pcInfoLabel = new QLabel(this);
    m_pcInfoLabel->move(380,80);
    m_pcInfoLabel->setText(tr("DynTxt"));
    m_pcInfoBtn = new QPushButton(this);
    m_pcInfoBtn->move(580,80);
    m_pcInfoBtn->setText(tr("DynBtn"));


    connect(m_pcInfoBtn,SIGNAL(pressed()), this, SLOT(ShowPcInfo()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::InitLang(QApplication *hApp, QTranslator *translator)
{
    m_hApp = hApp;
    m_translator = translator;
}
void MainWindow::TransToChinese(bool isValid){
    ui->actionChinese->setChecked(!ui->actionChinese->isChecked());
    if (!isValid) {
        return ;
    } else {
        ui->actionEngLish->setChecked(false);
    }
    TransCore("zh_CN");
}
void MainWindow::TransToEnglish(bool isValid)
{
    ui->actionEngLish->setChecked(!ui->actionEngLish->isChecked());
    if (!isValid) {
        return ;
    } else {
        ui->actionChinese->setChecked(false);
    }
    TransCore("en_US");
}

void MainWindow::TransCore(const QString &locale)
{
    const QString baseName = "QtLang_" + QLocale(locale).name();
    QString fileName = ":/i18n/" + baseName + ".qm";
    QFile langFile(fileName);
    if (!langFile.exists()) {
        QMessageBox::critical(this,"lang",fileName);
        return;
    }
    if (m_translator->load(fileName)) {
        m_hApp->installTranslator(m_translator);
        ui->retranslateUi(this);
        m_pcInfoLabel->setText(tr("DynTxt"));
        m_pcInfoBtn->setText(tr("DynBtn"));
    }
}

void MainWindow::TransToNext()
{
    static bool status = false;
    status = !status;
    if (status) {
        TransCore("zh_CN");
    } else {
        TransCore("en_US");
    }
}

void MainWindow::ShowPcInfo()
{
    QSharedPointer<PcInfo> pcInfo  = QSharedPointer<PcInfo>::create();
    QString pcInfoStr = pcInfo->GetCpuId() + ":CPU_ID \n";
    pcInfoStr += pcInfo->GetHardDiskId() + ":DISK_ID \n";
    QMessageBox::critical(this,__FUNCTION__, pcInfoStr);
}
